package models;

import java.util.ArrayList;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import play.db.jpa.Model;

@Entity
public class Author extends Model {

	public String firstName;
	public String lastName;
	
	@ManyToMany(mappedBy="authors")
	public List<Book> books = new ArrayList<Book>();
	
	
	public Author() {
		super();
	}
	
	public Author(String firstName, String lastName) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	@Override
	public String toString() {
		return "" + firstName + " " + lastName;
	}
	
	public Author saveOrLoad(){
		if (id != null)
			return this;
		Author result = find("byFirstNameAndLastName", firstName, lastName).first();
		if (result != null){
			this.id = result.id;
			return result;
		}
		save();
		return this;
	}
}
